﻿using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SkiaSharp;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView;

namespace TCOChart
{
    public partial class FormPieFailure : UserControl
    {
        public IEnumerable<ISeries> Series { get; set; }
        public List<string> CriticalInsights { get; } = new List<string>();
        public double TotalDowntimeHours { get; private set; }
        public string SummaryTitle { get; private set; } = "设备故障原因分析";

        public List<FailureCategory> failureData;

        public FormPieFailure()
        {
            InitializeComponent();


            InitPie();
        }


        void InitPie()
        {
            failureData = new List<FailureCategory>
            {
                new FailureCategory { Cause = "电气系统故障", FailureCount = 22, TotalDowntimeHours = 38.2, AvgRepairHours = 1.74 },
                new FailureCategory { Cause = "机械部件磨损", FailureCount = 18, TotalDowntimeHours = 45.0, AvgRepairHours = 2.5 },
                new FailureCategory { Cause = "控制程序错误", FailureCount = 12, TotalDowntimeHours = 24.0, AvgRepairHours = 2.0 },
                new FailureCategory { Cause = "润滑系统问题", FailureCount = 8, TotalDowntimeHours = 12.8, AvgRepairHours = 1.6 },
                new FailureCategory { Cause = "操作人员失误", FailureCount = 5, TotalDowntimeHours = 15.0, AvgRepairHours = 3.0 },
                new FailureCategory { Cause = "环境温度过高", FailureCount = 3, TotalDowntimeHours = 11.5, AvgRepairHours = 3.83 }
            };

            TotalDowntimeHours = 0;
            foreach (var item in failureData) TotalDowntimeHours += item.TotalDowntimeHours;

            var industrialColors = new[]
            {
                SKColor.Parse("#FF6B6B"),    // 电气 - 红色
                SKColor.Parse("#4ECDC4"),    // 机械 - 青色
                SKColor.Parse("#FFD166"),    // 控制系统 - 黄色
                SKColor.Parse("#06D6A0"),    // 润滑问题 - 绿色
                SKColor.Parse("#118AB2"),    // 操作失误 - 蓝色
                SKColor.Parse("#9B5DE5")     // 环境因素 - 紫色
            };

            var series = new List<ISeries>();

            for (int i = 0; i < failureData.Count; i++)
            {
                var data = failureData[i];
                var color = industrialColors[i];

                var failureSeries = new PieSeries<ObservableValue>
                {
                    Values = new[] { new ObservableValue(data.FailureCount) },
                    Name = data.Cause,
                    Fill = new SolidColorPaint(color),
                    Stroke = new SolidColorPaint(SKColors.White, 1.5f),
                    //OuterRadiusOffset = i == 0 ? 15 : 0, // 最大占比突出显示
                    DataLabelsPosition = LiveChartsCore.Measure.PolarLabelsPosition.Outer,
                    DataLabelsPaint = new SolidColorPaint(SKColors.Navy),
                    DataLabelsSize = 10,
                    DataLabelsFormatter = point =>
                        $"{data.Cause + Environment.NewLine} ({point.StackedValue.Share * 100:F1}%)",
                    ToolTipLabelFormatter = (point) =>
                        $"**{data.Cause}**" + Environment.NewLine +
                        $"故障次数:{data.FailureCount}" + Environment.NewLine +
                        $"停机时长:{data.TotalDowntimeHours:F1}小时" + Environment.NewLine +
                        $"平均修复:{data.AvgRepairHours:F1}小时" + Environment.NewLine +
                        $"成本损失:¥{data.TotalDowntimeHours * data.CostPerHour:N0}" + Environment.NewLine +
                        $"占比:{point.StackedValue.Share * 100:F1}%"
                };

                data.ChartSeries = failureSeries;
                series.Add(failureSeries);
            }

            Series = series;

            pieChart1.Series = Series;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            failureData[0].FailureCount += 1;

            // 2. 更新图表数据源
            var electricalSeries = (PieSeries<ObservableValue>)failureData[0].ChartSeries;
            electricalSeries.Values.First().Value = failureData[0].FailureCount;

            // 3. 更新统计数据和相关信息
            failureData[0].TotalDowntimeHours += failureData[0].AvgRepairHours;
            TotalDowntimeHours = failureData.Sum(item => item.TotalDowntimeHours);

            pieChart1.Update();
        }
    }


    public class FailureCategory
    {
        public string Cause { get; set; }
        public int FailureCount { get; set; }
        public double TotalDowntimeHours { get; set; }
        public double AvgRepairHours { get; set; }
        public int CostPerHour { get; } = 5000; // 每小时生产成本损失(元)

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ISeries ChartSeries { get; set; } // 关联的图表系列
    }
}
